<?php
/**
 * [JishiGou] (C)2005 - 2099 Cenwor Inc.
 *
 * This is NOT a freeware, use is subject to license terms
 *
 * @Filename dig.mod.php $
 *
 * @Author http://www.jishigou.net $
 *
 * @Date 2013-05-21 17:04:54 763362684 894541331 9705 $
 */
if (! defined ( 'IN_JISHIGOU' )) {
	exit ( 'invalid request' );
}
class ModuleObject extends MasterObject {
	var $member;
	function ModuleObject($config) {
		$this->MasterObject ( $config );
		$this->member = $this->TopicLogic = jlogic ( 'topic' );
		ob_start ();
		switch ($this->Code) {
			case 'dig' :
				$this->dig ();
				break;
			case 'ajax' :
				$this->getajax ();
				break;
			case 'user' :
				$this->getuser ();
				break;
			case 'rcduser' :
				$this->getrcduser ();
				break;
			default :
				$this->Main ();
				break;
		}
		response_text ( ob_get_clean () );
	}
	function Main() {
		response_text ( "error" );
	}
	function dig() {
		$tid = jget ( 'tid', 'int', 'P' );
		$uid = jget ( 'uid', 'int', 'P' );
		if ($tid > 0 && $uid > 0) {
			$count = DB::result_first ( "SELECT COUNT(*) FROM " . DB::table ( 'topic_dig' ) . " WHERE tid='{$tid}' AND uid = '" . MEMBER_ID . "'" );
			if ($count > 0) {
				echo 'no,';
			} else {
				$uid = DB::result_first ( "SELECT uid FROM " . DB::table ( 'topic' ) . " WHERE tid='{$tid}'" );
				if ($uid == MEMBER_ID) {
					echo 'not,';
				} else {
					jtable ( 'topic_more' )->update_diguids ( $tid );
					DB::query ( "update `" . DB::table ( 'members' ) . "` set `digcount` = digcount + 1,`dig_new` = dig_new + 1 where `uid`='{$uid}'" );
					$ary = array (
							'tid' => $tid,
							'uid' => MEMBER_ID,
							'touid' => $uid,
							'dateline' => time () 
					);
					DB::insert ( 'topic_dig', $ary, true );
					jtable ( 'topic' )->update_digcounts ( $tid );
					$credits = ConfigHandler::get ( 'credits' );
					update_credits_by_action ( 'topic_dig', MEMBER_ID );
					update_credits_by_action ( 'my_dig', $uid );
					echo 'yes,' . $this->js_show_msg ( 1 );
				}
			}
		}
	}
	function getajax() {
		$cachefile = ConfigHandler::get ( 'channel' );
		$channel_channels = is_array ( $cachefile ['channels'] ) ? $cachefile ['channels'] : array ();
		$type = in_array ( $this->Post ['type'], array (
				'recommend_top',
				'week_dig',
				'month_dig',
				'dig_user_follow',
				'recommend',
				'recommend20' 
		) ) ? $this->Post ['type'] : '';
		$itemid = jget ( 'id', 'int', 'P' );
		$uid = jget ( 'uid', 'int', 'P' );
		$num = jget ( 'num', 'int', 'P' );
		if ($itemid && $channel_channels [$itemid]) {
			$ids = implode ( ",", $channel_channels [$itemid] );
			$where = " AND `item_id` IN(" . jimplode ( $channel_channels [$itemid] ) . ") ";
		} else {
			$where = "";
		}
		$time = TIMESTAMP;
		$topic_list = array ();
		if ($type == 'month_dig') {
			$time = $time - 30 * 24 * 3600;
			$cache_id = 'dig-30days-top10-' . $itemid;
		} elseif ($type == 'week_dig') {
			$time = $time - 7 * 24 * 3600;
			$cache_id = 'dig-7days-top10-' . $itemid;
		} elseif ($type == 'recommend_top') {
			$cache_id = 'channel-recommend-top10-' . $itemid;
		} elseif ($type == 'dig_user_follow') {
			$cache_id = 'dig_user_follow-' . $itemid;
		} elseif ($type == 'recommend') {
			$cache_id = 'all-recommend-top10';
		} elseif ($type == 'recommend20') {
			$cache_id = 'all-recommend-top20';
		} else {
			echo '<font color=red>开发中......</font>';
			exit ();
		}
		$topic_list = cache_file ( 'get', $cache_id );
		if (empty($topic_list)) {
			$isquery = true;
			if ($type == 'dig_user_follow') {
				$query = DB::query ( "SELECT uid FROM " . TABLE_PREFIX . "topic_dig WHERE tid = '" . $itemid . "'" );
				while ( $rs = DB::fetch ( $query ) ) {
					$diguids [] = $rs ['uid'];
				}
				$query = DB::query ( "SELECT tid,count(tid) as nums FROM " . TABLE_PREFIX . "topic_dig WHERE tid<>'" . $itemid . "' AND uid IN(" . jimplode ( $diguids ) . ") group by tid ORDER BY nums DESC LIMIT 10" );
				while ( $rs = DB::fetch ( $query ) ) {
					$digftids [] = $rs ['tid'];
				}
				if (count ( $digftids ) == 0) {
					$isquery = false;
				}
				$sql = "SELECT `tid`,`content` FROM `" . TABLE_PREFIX . "topic` WHERE `type` = 'first' AND tid IN(" . jimplode ( $digftids ) . ") ORDER BY digcounts DESC ";
			} elseif ($type == 'recommend_top') {
				$sql = "SELECT tid,r_title as content FROM `" . TABLE_PREFIX . "topic_recommend` WHERE r_title <> '' AND item = 'channel' " . $where . " and (expiration>" . time () . " OR expiration=0) ORDER BY recd DESC,dateline DESC LIMIT 10 ";
			} elseif ($type == 'recommend') {
				$sql = "SELECT tid,r_title as content FROM `" . TABLE_PREFIX . "topic_recommend` WHERE r_title <> '' AND (expiration>" . time () . " OR expiration=0) ORDER BY recd DESC,dateline DESC LIMIT 10 ";
			} elseif ($type == 'recommend20') {
				$sql = "SELECT tid,r_title as content FROM `" . TABLE_PREFIX . "topic_recommend` WHERE r_title <> '' AND (expiration>" . time () . " OR expiration=0) ORDER BY recd DESC,dateline DESC LIMIT 20 ";
			} else {
				$sql = "SELECT `tid`,`content` FROM `" . TABLE_PREFIX . "topic`
					WHERE `digcounts` > 0 " . $where . " AND `dateline` >= $time AND `type` = 'first'
					ORDER BY `digcounts` DESC, `lastdigtime` DESC LIMIT 10 ";
			}
			$topic_list = array ();
			if ($isquery) {
				$query = DB::query ( $sql );
				while ( $rs = DB::fetch ( $query ) ) {
					if ($type == 'month_dig' || $type == 'week_dig') {
						$userdata = $this->_getuserfortid ( $rs ['tid'] );
						$rs ['uid'] = $userdata ['uid'];
						$rs ['username'] = $userdata ['username'];
						$rs ['nickname'] = $userdata ['nickname'];
						$rs ['face'] = $userdata ['face'];
					}
					$rs ['content'] = cut_str ( strip_tags ( $rs ['content'] ), 36 );
					$topic_list [$rs ['tid']] = $rs;
				}
			}
			cache_file ( 'set', $cache_id, $topic_list, 36000 );
		}
		if ($type == 'month_dig' || $type == 'week_dig') {
			$html = '';
			if ($topic_list) {
				$i = 1;
				foreach ( $topic_list as $rs ) {
					$html .= "<li><i class='hotlist{$i}'>{$i}</i><div class='list_face'><img onerror='javascript:faceError(this);' src='{$rs['face']}'></div><div class='list_con'><p><a href='" . jurl ( "index.php?mod={$rs['username']}" ) . "'>{$rs['nickname']}</a></p><p><a href='" . jurl ( "index.php?mod=topic&code={$rs[tid]}" ) . "'>{$rs['content']}</a></p></div></li>";
					$i ++;
					if ($i > $num) {
						break;
					}
				}
			} else {
				$html .= '没有找到相关' . $this->Config ['changeword'] ["n_weibo"];
			}
		} else {
			$html = '<ul class="hot_reply_b">';
			if ($topic_list) {
				$i = 1;
				foreach ( $topic_list as $rs ) {
					$html .= "<li><a href='" . jurl ( "index.php?mod=topic&code={$rs[tid]}" ) . "' title='点此查看详情' target='_blank'>{$i}.{$rs['content']}</a></li>";
					$i ++;
				}
			} else {
				$html .= '没有找到相关' . $this->Config ['changeword'] ["n_weibo"];
			}
			$html .= '</ul>';
		}
		echo $html;
	}
	function getuser() {
		$tid = jget ( 'tid', 'int', 'P' );
		$num = jget ( 'num', 'int', 'P' );
		if ($tid > 0 && $num > 0) {
			$dig_users = array ();
			$user = $this->TopicLogic->GetDigUids ( $tid );
			if ($user && is_array ( $user ) && count ( $user ) == $num) {
				if ($num > 5) {
					$i = $num - 5;
					$user = array_slice ( $user, $i, 5 );
				}
			} else {
				$query = DB::query ( "SELECT uid FROM `" . TABLE_PREFIX . "topic_dig`	WHERE tid = '" . $tid . "' ORDER BY id DESC LIMIT 5" );
				while ( $rs = DB::fetch ( $query ) ) {
					$user [$rs ['uid']] = $rs ['uid'];
				}
				$user = array_slice ( $user, 0, 5 );
			}
			$dig_users = $this->TopicLogic->GetMember ( $user, "`uid`,`username`,`nickname`,`face`" );
			foreach ( $dig_users as $k => $v ) {
				array_splice ( $dig_users [$k], 4, 3 );
			}
			$html = '<script type="text/javascript">$(document).ready(function(){$(".digusername").bind("mouseover", function(){$(this).show();});$(".digusername").bind("mouseout", function(){$(this).hide();});});</script>';
			$html .= '<div class="digusername"><div class="list"><p>' . $this->Config ['changeword'] ['dig'] . '者</p><ul>';
			foreach ( $dig_users as $u ) {
				$html .= '<li><a href="' . jurl ( 'index.php?mod=' . $u ['username'] ) . '"><img src="' . $u ['face'] . '" onerror="javascript:faceError(this);" onmouseover="get_dig_user_choose(\'' . $u ['nickname'] . '\',' . $u ['uid'] . ',' . $tid . ')"></a><p><a href="index.php?mod=' . $u ['username'] . '" onmouseover="get_dig_user_choose(\'' . $u ['nickname'] . '\',' . $u ['uid'] . ',' . $tid . ')">' . $u ['nickname'] . '</a></p></li>';
			}
			$html .= '<span style="clear:both;" /></ul></div><div id="diguserface"></div></div>';
			echo $html;
		}
	}
	function getrcduser() {
		$tid = jget ( 'tid', 'int', 'P' );
		if ($tid > 0) {
			$rcduser = DB::fetch_first ( "SELECT dateline,r_nickname FROM " . DB::table ( 'topic_recommend' ) . " WHERE tid='{$tid}'" );
			$html = '<script type="text/javascript">$(document).ready(function(){$(".rcdusername").bind("mouseover", function(){$(this).show();});$(".rcdusername").bind("mouseout", function(){$(this).hide();});});</script>';
			$html .= '<div class="rcdusername"><div style="text-align:center;background:#fdffd2;border:1px solid #ffecb0;padding:1px 10px;width:380px;overflow:hidden; margin:3px 0 0 80px"><p>推荐人：' . $rcduser ['r_nickname'] . '，推荐时间：' . my_date_format ( $rcduser ['dateline'], "Y-m-d H:i" ) . '，<a href="' . jurl ( 'index.php?mod=topic&code=recd' ) . '">查看更多推荐</a></p></div></div>';
			echo $html;
		}
	}
	function _getuserfortid($tid = 0) {
		if ($tid > 0) {
			$user = DB::fetch_first ( "SELECT t.uid,m.username,m.nickname FROM " . DB::table ( 'topic' ) . " AS t LEFT JOIN " . DB::table ( 'members' ) . " AS m ON t.uid = m.uid WHERE t.tid='{$tid}'" );
			$user ['face'] = face_get ( $user ['uid'] );
			return $user;
		}
	}
}
?>